package chenji.gunapi.listeners;

import java.util.HashSet;
import org.bukkit.entity.Player;

import chenji.gunapi.GunAPI;
import chenji.gunapi.manager.Manager;
import chenji.gunapi.task.Taskable;

public class PlayerStateListener extends Manager implements Taskable {
	private HashSet<Player> sitingPlayer = new HashSet<>();
	private HashSet<Player> moving = new HashSet<Player>();

	public PlayerStateListener() {
		GunAPI.getGunAPI().getGunTaskManager().unregisterPerTickTask(this);
		this.load();
	}

	@Override
	public void task() {
		this.moving.clear();
	}

	@Override
	public void onPlayerQuit(Player p) {
		this.moving.remove(p);
		this.sitingPlayer.remove(p);
	}

	@Override
	public void onDisable() {

	}

	@Override
	public void onPlayerJoin(Player p) {
	}

	public void onPlayerSit(Player p) {
		if (this.sitingPlayer.contains(p)) {
			this.sitingPlayer.remove(p);
		} else {
			this.sitingPlayer.add(p);
		}
	}

	private boolean isMoving(Player p) {
		return this.moving.contains(p);
	}

	private boolean isSiting(Player p) {
		return this.sitingPlayer.contains(p);
	}

	public PlayerState getState(Player p) {
		if (this.isMoving(p)) {
			if (this.isSiting(p)) {
				return PlayerState.WALKINGANDSITING;
			} else {
				return PlayerState.WALKING;
			}
		}
		if (this.isSiting(p)) {
			return PlayerState.SITING;
		} else {
			return PlayerState.STATIC;
		}
	}

	public enum PlayerState {
		STATIC, WALKING, SITING, WALKINGANDSITING;
	}

}
